/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.converter;

import org.ipti.br.tag.dao.CampaignFacade;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Unity;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.inject.Inject;

/**
 *
 * @author ipti
 */
@FacesConverter(value="campaignC", forClass=Unity.class)
public class CampaignConverter implements Converter{

    @Inject private CampaignFacade campaignDAO;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if(value == null || value.trim().isEmpty() || "".equals(value)) return null;
        
        return campaignDAO.find(Long.parseLong(value));
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if(value == null) return "";
        
        return ((Campaign) value).getId().toString();
    }
}
